package com.qf.dao.impl;

import com.qf.dao.OrderDetailDao;
import com.qf.entity.OrderDetail;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package com.qf.dao.impl
 * @description: ${TODO}
 * @date 2022/6/27 9:36
 */
public class OrderDetailDaoImpl implements OrderDetailDao {
    @Override
    public void insert(OrderDetail orderDetail) {
        QueryRunner qr=new QueryRunner();
        Connection conn= DataSourceUtils.getConnection();
        Object[] params={orderDetail.getOid(),orderDetail.getPid(),orderDetail.getNum(),orderDetail.getMoney()};
        try {
            qr.update(conn,"insert into tb_orderdetail values(null,?,?,?,?);",params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null, null, conn);
        }
    }

    @Override
    public List<OrderDetail> selectByOid(String oid) {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        try {
            return qr.query("select * from tb_orderdetail where oid=?", new BeanListHandler<>(OrderDetail.class),oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
